﻿using GatewaySwagger;
using System.Text;

namespace GatewayUI
{
    public static class ApplicationBuilderExtension
    {
        public static string GetDefaultPageContent(this WebApplication app)
        {
            var configration = app.Configuration;
            var env = app.Environment;

            var outputHtml = new StringBuilder(500);
            outputHtml.Append("<div align='center'>");
            outputHtml.Append(@"<a href='' target='_blank'><img src='yande.png'/></a>");
            outputHtml.Append($"<br><br>ASPNETCORE_ENVIRONMENT={env.EnvironmentName}");
            var serviceRoters = configration.GetSection("Routes").Get<List<ServiceRouter>>();

            if (serviceRoters is not null)
            {
                outputHtml.Append($"<br>--------------------------------------------------------------------------------------------<br>");

                var services = serviceRoters.Select(p => p.ServiceName) .Distinct();

                foreach (var service in services)
                {
                    //服务名称需要优化的问题
                    outputHtml.Append($"<a href='{service.Replace(".api", "")}/swagger/index.html' target='_blank'>{service}</a>&nbsp;&nbsp;<b>");
                }

            }
            outputHtml.Append($"<br><br>{DateTime.Now}");
            outputHtml.Append("</div>");
            return outputHtml.ToString();
        }
    }
}
